<template>:span="24"
    <el-drawer v-model="isShow" :title="title" :destroy-on-close="true" size="50%" :close-on-press-escape="false"
        :close-on-click-modal="false" :show-close="false">
        <template #header="{ close, titleId, titleClass }">
            <h4 :id="titleId" :class="titleClass">{{ title }}</h4>
        </template>
        <el-form :model="form" label-width="auto" ref="formRef" :rules="rules">
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="房间名称" required prop="roomcode">
                        <el-input v-model="form.roomcode" placeholder="请输入房间编号" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="房间编号" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="房间类别" required prop="roomtypeId">
                        <el-select v-model="form.roomtypeId">
                            <el-option v-for="item in RoomTypeList" :key="item.roomtypeId" :label="item.name"
                                :value="item.roomtypeId" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="所属单位" required prop="unitId">
                        <el-tree-select v-model="form.unitId" :data="deptOptions" 
                            :props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
                            placeholder="选择上级部门" check-strictly @node-click="deptNameClick" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="所在办案区" required prop="policeareaId">
                        <el-select v-model="form.policeareaId">
                            <el-option v-for="item in PoliceAreaList" :key="item.policeareaId" :label="item.areaname"
                                :value="item.policeareaId" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="状态" required>
                        <el-radio-group v-model="form.state">
                            <el-radio v-for="(item, index) in sys_normal_disable" :key="'state' + index"
                                :value="item.value">{{
                                    item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <el-button type="primary" @click="submitHandle">提交</el-button>
            <el-button @click="isShow = false">关闭</el-button>
        </template>
    </el-drawer>
</template>

<script setup>
import { getCurrentInstance } from "vue";
// import { createRoom, editRoom, getRoomInfo, getRoomTypeList } from "@/api/Room/room";
import { listDept, } from "@/api/system/dept";
import { getUserProfile } from "@/api/system/user";
// import { getPoliceAreaList, } from "@/api/PoliceAreaManage/PoliceAreaManage";

const { proxy } = getCurrentInstance()
const { sys_normal_disable, nj_room_type } = proxy.useDict('sys_normal_disable', 'nj_room_type')
// 是否显示抽屉
const isShow = ref(false)
// 抽屉标题
const title = ref('')
// 部门
const department = ref([])
// 所属单位
const deptOptions = ref([]);
// 办案区列表
const PoliceAreaList = ref([]);
// 房间类型
const RoomTypeList = ref([]);
// 通知事件
const emit = defineEmits(['getRoomList'])

const rules = {
    roomcode: [{ required: true, message: "房间编号不能为空", trigger: "blur" }],
    roomname: [{ required: true, message: "房间名称不能为空", trigger: "blur" }],
    unitId: [{ required: true, message: "所属单位不能为空", trigger: "blur" }],
    policeareaId: [{ required: true, message: "所在警务区不能为空", trigger: "blur" }],
    roomtypeId: [{ required: true, message: "房间类别不能为空", trigger: "blur" }]
}

// 房间
const form = ref({
    roomname: '房间1',
    roomcode: '1',
    roomtypeId: '1',
    unitId: 103,
    policeareaId: '',
    state: '0',
})

// 关闭后回调
const defaultQuery = {
    roomname: '',
    roomcode: '',
    roomtypeId: '',
    unitId: null,
    policeareaId: '',
    state: '0',
}
const deptqueryParams = ref({
    status: undefined,
})
const changeShowStatus = () => {
    isShow.value = !isShow.value
}

onMounted(() => {
   
})
// 获取个人信息
const getUserInfo = async () => {
    await getUserProfile().then(res => {
        form.value.unitId = res.data.unitId
    })
}
// 添加房间
const showAddDrawer = (data_deptId) => {
    title.value = '添加房间';
    reset();
    getDepartment(data_deptId)
    getAreaLists()
    changeShowStatus()
}

// 修改房间 
const showEditDrawer = async (data) => {
    title.value = '修改房间信息'
    console.log(data)
    // await getRoomInfo(data.roomId).then(res => {
    //     // console.log(res.data.policeareaId)
    //     form.value = res.data
    //     form.value.unitId = Number(res.data.unitId)

    //     const Params = {
    //         unitId: form.value.unitId
    //     }
    //     // getPoliceAreaList(Params).then(res => {
    //     //     PoliceAreaList.value = res.rows
    //     // })

    // })

    // await listDept(deptqueryParams.value).then(res => {
    //     deptOptions.value = res.data.filter(
    //         item => item.isUnit === true
    //     );
    //     deptOptions.value = proxy.handleTree(deptOptions.value, "deptId");
    // })
    // getDepartment(Number(data.unitId))
    // getRoomTypeLists()
    changeShowStatus()
}
defineExpose({ showAddDrawer, showEditDrawer })

// 警务区
const AreaListqueryParams = ref({
    areaname: '',
    unitId: '',
    areatype: null,
})
// 房间类型
const RoomTypeListqueryParams = ref({
    roomtypeId: '',
    name: '',
    type: 1,
})

// 表格属性
const Areapaging = ref({
    pageNum: 1,// 页码
    pageSize: 9999// 分页数量
})

// 获取警务区列表
const getAreaLists = async () => {
    // await getPoliceAreaList(AreaListqueryParams.value, Areapaging.value).then(res => {
    //     PoliceAreaList.value = res.rows
    // })
}

// 获取房间类型列表
const getRoomTypeLists = async () => {
    // await getRoomTypeList(RoomTypeListqueryParams.value, Areapaging.value).then(res => {
    //     RoomTypeList.value = res.rows
    // })
}
//获取单位（部门）列表
const getDepartment = async (data_deptId) => {
    console.log(data_deptId)
    if (data_deptId == "") {
        getUserInfo();      //找当前登录用户所在部门     
    } else {
        form.value.unitId = data_deptId
        AreaListqueryParams.value.unitId = data_deptId
    }

    await listDept(deptqueryParams.value).then(res => {
        deptOptions.value = res.data.filter(
            item => item.isUnit === true
        );
        deptOptions.value = proxy.handleTree(deptOptions.value, "deptId");
    })
}

/** 节点单击事件 */
function deptNameClick(data) {
    AreaListqueryParams.value.unitId = data.deptId;
    form.value.policeareaId = "";
    // console.log(data.deptId);
    getAreaLists();
};


const submitHandle = () => {
    // 必输校验--标签<el-form :model="form" label-width="auto" ref="formRef"  :rules="rules"> 添加ref="formRef" 属性 :rules="rules"绑定
    // 然后const rules = {里面是需要的rules}   在每个<el-form-item label="房间编号" required prop="对应字段">加上 prop="对应的字段"
    // submitHandle的时候先判断    proxy.$refs["formRef"].validate(valid => {
    // if (valid) {  valid true面执行具体代码逻辑     });}

    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            if (form.value['roomId']) {
                editRoom(form.value).then(res => {
                    changeShowStatus()
                    emit('getRoomList', '来了老弟')
                })
            } else {
                createRoom(form.value).then(res => {
                    changeShowStatus()
                    emit('getRoomList', '来了老弟')
                })
            }
        }

    });
}

// 关闭后的回调事件
const closeHandle = () => {
    form.value = defaultQuery
}
/** 重置操作表单 */
function reset() {
    form.value = {
        roomname: undefined,
        roomcode: undefined,
        roomtypeId: undefined,
        unitId: undefined,
        policeareaId: undefined,
        state: '0',
    };
    proxy.resetForm("formRef");
};
/** 取消按钮 */
const cancel = () => {
    isShow.value = false
    // form.value = defaultQuery
    reset();
}

</script>

<style lang="scss" scoped>
h4 {
    margin: 10px 0;
}
</style>